package org.xuyuji.web.dto;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Menu {
	private int menuId;
	
	private String menuName;
	
	private String url;
	
	private int parentId;
	
	private int sort;
	
	private List<Menu> childs = new ArrayList<Menu>();

	public int getMenuId() {
		return menuId;
	}

	public void setMenuId(int menuId) {
		this.menuId = menuId;
	}

	public String getMenuName() {
		return menuName;
	}

	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getParentId() {
		return parentId;
	}

	public void setParentId(int parentId) {
		this.parentId = parentId;
	}

	public int getSort() {
		return sort;
	}

	public void setSort(int sort) {
		this.sort = sort;
	}
	
	public void addChild(Menu child){
		this.childs.add(child);
	}
	
	public List<Menu> getChilds(){
		List<Menu> list = new ArrayList<Menu>(Arrays.asList(new Menu[childs.size()]));
		Collections.copy(list, childs);
		return list;
	}
	
	@Override
	public String toString() {
		return toHtmlTree();
	}
	
	private String toJson(){
		StringBuilder sb = new StringBuilder();
		
		sb.append("{");
		sb.append("menuId:").append("'").append(menuId).append("',");
		sb.append("menuName:").append("'").append(menuName).append("',");
		sb.append("url:").append("'").append(url).append("',");
		sb.append("parentId:").append("'").append(parentId).append("',");
		sb.append("sort:").append("'").append(sort).append("',");
		if(!childs.isEmpty()){
			sb.append("childs:").append("[");
			int i = 0;
			for(; i < childs.size() - 1; i++){
				sb.append(childs.get(i)).append(",");
			}
			sb.append(childs.get(i));
			sb.append("]");
		}
		sb.append("}");
		
		return sb.toString();
	}
	
	private String toHtmlTree(){
		StringBuilder sb = new StringBuilder();
		
		sb.append("<li><span>+</span><a href='").append(url).append("' target='main'>").append(menuName).append("</a>");
		if(!childs.isEmpty()){
			sb.append("<ul>");
			int i = 0;
			for(; i < childs.size(); i++){
				sb.append(childs.get(i));
			}
			sb.append("</ul>");
		}
		sb.append("</li>");
		
		return sb.toString();
	}
}
